package io.gitee.zhangbinhub.admin.oauth.controller.api

import cn.dev33.satoken.annotation.SaCheckPermission
import io.gitee.zhangbinhub.acp.boot.exceptions.WebException
import io.gitee.zhangbinhub.acp.boot.log.LogAdapter
import io.gitee.zhangbinhub.acp.core.common.CommonTools
import io.gitee.zhangbinhub.admin.common.api.OauthApi
import io.gitee.zhangbinhub.admin.common.constant.ModuleFuncCode
import io.gitee.zhangbinhub.admin.common.constant.RoleCode
import io.gitee.zhangbinhub.admin.common.vo.CustomerQueryPageVo
import io.gitee.zhangbinhub.admin.common.vo.InfoVo
import io.gitee.zhangbinhub.admin.oauth.bus.publish.ReloadDataEventPublish
import io.gitee.zhangbinhub.admin.oauth.controller.openinner.OpenInnerRuntimeController
import io.gitee.zhangbinhub.admin.oauth.entity.RuntimeConfig
import io.gitee.zhangbinhub.admin.oauth.po.RuntimePo
import io.gitee.zhangbinhub.admin.oauth.po.RuntimeQueryPo
import io.gitee.zhangbinhub.admin.oauth.service.RuntimeConfigService
import io.gitee.zhangbinhub.admin.resource.server.base.BaseResourceServerController
import io.gitee.zhangbinhub.admin.resource.server.vo.RuntimeConfigVo
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import io.swagger.annotations.ApiParam
import org.noear.solon.annotation.*
import org.noear.solon.core.handle.MethodType
import org.noear.solon.core.util.MimeType
import org.noear.solon.validation.annotation.NotEmpty
import org.noear.solon.validation.annotation.NotNull
import org.noear.solon.validation.annotation.Valid
import org.noear.solon.validation.annotation.Validated

@Valid
@Controller
@Api("运行参数配置")
open class RuntimeController(
    @Inject logAdapter: LogAdapter,
    private val openInnerRuntimeController: OpenInnerRuntimeController,
    private val runtimeConfigService: RuntimeConfigService,
    private val reloadDataEventPublish: ReloadDataEventPublish
) : BaseResourceServerController(logAdapter) {

    @ApiOperation("新建参数信息", notes = "参数名称、参数值、描述、状态")
    @SaCheckPermission(ModuleFuncCode.runtimeAdd, orRole = [RoleCode.SUPER])
    @Mapping(
        method = [MethodType.PUT],
        value = OauthApi.basePath + OauthApi.runtimeConfig,
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    @Throws(WebException::class)
    fun add(@Body @Validated runtimePo: RuntimePo): RuntimeConfig = runtimeConfigService.doCreate(runtimePo).also {
        reloadDataEventPublish.doNotifyRefreshRuntime()
    }

    @ApiOperation("删除指定的参数信息")
    @SaCheckPermission(ModuleFuncCode.runtimeDelete, orRole = [RoleCode.SUPER])
    @Mapping(
        method = [MethodType.DELETE],
        value = OauthApi.basePath + OauthApi.runtimeConfig,
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    fun delete(
        @ApiParam("id列表", required = true)
        @NotEmpty(message = "id不能为空")
        @NotNull(message = "id不能为空")
        @Body
        idList: MutableList<String>
    ): InfoVo = runtimeConfigService.doDelete(idList).also {
        reloadDataEventPublish.doNotifyRefreshRuntime()
    }.let { InfoVo(message = "删除成功") }

    @ApiOperation("更新指定的参数信息", notes = "可更新参数值、描述、状态")
    @SaCheckPermission(ModuleFuncCode.runtimeUpdate, orRole = [RoleCode.SUPER])
    @Mapping(
        method = [MethodType.PATCH],
        value = OauthApi.basePath + OauthApi.runtimeConfig,
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    @Throws(WebException::class)
    fun update(@Body @Validated runtimePo: RuntimePo): RuntimeConfig {
        if (CommonTools.isNullStr(runtimePo.id)) {
            throw WebException("配置ID不能为空")
        }
        return runtimeConfigService.doUpdate(runtimePo).also {
            reloadDataEventPublish.doNotifyRefreshRuntime()
        }
    }

    @ApiOperation("查询参数信息列表", notes = "查询条件：参数名称、值、状态")
    @SaCheckPermission(ModuleFuncCode.runtimeQuery, orRole = [RoleCode.SUPER])
    @Mapping(
        method = [MethodType.POST],
        value = OauthApi.basePath + OauthApi.runtimeConfig,
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    @Throws(WebException::class)
    fun query(@Body @Validated runtimeQueryPo: RuntimeQueryPo): CustomerQueryPageVo<RuntimeConfig> =
        runtimeConfigService.doQuery(runtimeQueryPo)

    @ApiOperation("获取参数信息", notes = "根据参数名称获取")
    @Mapping(
        method = [MethodType.GET],
        value = OauthApi.basePath + OauthApi.runtimeConfig + "/{name}",
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    @Throws(WebException::class)
    fun find(@Path(name = "name") name: String): RuntimeConfigVo =
        openInnerRuntimeController.find(name)

    @ApiOperation("获取参数信息", notes = "根据参数名称获取")
    @Mapping(
        method = [MethodType.GET],
        value = OauthApi.basePath + OauthApi.runtimeConfig,
        produces = MimeType.APPLICATION_JSON_VALUE
    )
    @Throws(WebException::class)
    fun findList(): Map<String, RuntimeConfigVo> = openInnerRuntimeController.findList()

}
